package com.baosight.sczx.zm.cg.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department; 
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCP;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cg.domain.ZMCGChangeWorkDetail;
import com.baosight.sczx.zm.cn.domain.ZMCNMccyMain;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanWM;
/**
 * <p>Title: ServiceZMCP02.java</p>
 * <p>Description: 文明生产计划明细</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张坤
 * @version 1.0
 * <pre>Histroy:
 *          2016-03-21 下午02:51:04  张坤  Create
 *</pre>
 */
public class ServiceZMCG4001 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午02:51:38 张坤
	 * </p>
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}	
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午02:51:58 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdChagneId = inInfo.getString("fdChagneId");
		EiInfo outInfo = inInfo;
		if(fdChagneId!=null)
		{ 
			StaticTools.setQueryParameters(inInfo, "fdChagneId", fdChagneId, true);//添加查询条件
			outInfo = super.query(inInfo, "ZMCGChangeWorkDetail.query", new ZMCGChangeWorkDetail());
		}	
		else{
			outInfo.addBlock("result").setBlockMeta(new ZMCPProducePlanWM().eiMetadata);
		}
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));		 
		return outInfo;
	}
		
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午03:07:57 张坤
	 * </p>
	 *
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 		
		String date = StaticTools.getDate();
		String fdOutperson = (String) UserSession.getInSessionProperty("UserCname");
		ZMCGChangeWorkDetail _dom = new ZMCGChangeWorkDetail();
		EiBlock eiBlock = inInfo.getBlock("inqu_status1");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		_dom.fromMap(eiBlock.getRow(0));
			
		inInfo.set("fdChagneId", _dom.getFdChagneId());
		_dom.setFdCurrentDate(date);
		_dom.setFdOutperson(fdOutperson);
		//_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
		_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_CHANGEWORK_DETAIL"));
		this.dao.insert("ZMCGChangeWorkDetail.insert",_dom); 
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作1记录成功");
		return out;  
	}		
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午04:20:50 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验是否重复
		EiInfo out=new EiInfo();
		String date = StaticTools.getDate();
		String fdOutperson = (String) UserSession.getInSessionProperty("UserCname");
		ZMCGChangeWorkDetail _dom = new ZMCGChangeWorkDetail();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		_dom.fromMap(eiBlock.getRow(0));
		inInfo.set("fdChagneId", _dom.getFdChagneId());
		_dom.setFdCurrentDate(date);
		_dom.setFdOutperson(fdOutperson);
		_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
		this.dao.update("ZMCGChangeWorkDetail.update",_dom);
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作1记录成功");
		return out;  
		
	}	
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午04:30:17 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) { 
		EiInfo _temp = super.delete(inInfo, "ZMCGChangeWorkDetail.delete");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo; 
	}	
	
	/**
     * <p>Function: delete</p>
	 * <p>Description: 删除</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2016-8-4 10:43:45 王伟
	 * </p>
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		ZMCGChangeWorkDetail _dom = new ZMCGChangeWorkDetail();
		EiBlock eiBlock = inInfo.getBlock("inqu_status1");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		String oper=(String)inInfo.get("efCurButtonEname");
		_dom.fromMap(eiBlock.getRow(0));
			
		Map<String, String> params = new HashMap<String, String>();
		String fdWorknumId = _dom.getFdWorknumId();
		params.put("fdWorknumId", fdWorknumId);
	    String fdVirtualDate = _dom.getFdVirtualDate();
		params.put("fdVirtualDate", fdVirtualDate);
		String fdChagneId = _dom.getFdChagneId();
		params.put("fdChagneId", fdChagneId);
		String fdOutperson = (String) UserSession.getInSessionProperty("UserCname");
		params.put("fdOutperson", fdOutperson);
		if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			params.put("fdKeyId", _dom.getFdKeyId());
		List result = dao.query("ZMCGChangeWorkDetail.validate", params);
		if(result != null && result.size() > 0) 
		{
			inInfo.setStatus(-1);
		    ZMCGChangeWorkDetail _temp = (ZMCGChangeWorkDetail)result.get(0);
			
		    StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdVirtualDate() != null )
			{
				String date = _temp.getFdVirtualDate();
				if(date.equals(fdVirtualDate))
				msg.append("调度日期 ");
			}
			if(_temp.getFdWorknumId() != null && _temp.getFdWorknumId().equals(fdWorknumId)) 
			{
				if(msg.equals(""))
				    msg.append("生产班次 ");
				else
					msg.append(",生产班次");
			}
			if(_temp.getFdOutperson() != null && _temp.getFdOutperson().equals(fdOutperson)) 
			{
				if(msg.equals(""))
				    msg.append("交接人 ");
				else
					msg.append(",交接人");
			}
		
		    msg.append("重复！");
		    inInfo.setMsg(msg.toString());
		    return inInfo;
		}
		inInfo.setStatus(1);
		return inInfo;
	}
}
